/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fontys.time;

/**
 *
 * @author Anh
 */
public class Period implements IPeriod {
    
    private ITime beginTime;
    private ITime endTime;
    private int length;
    /**
     * creation of a period with begin time bt and end time et
     * @param bt begin time bt must be earlier than end time et
     * @param et 
     */
    public Period(ITime bt, ITime et) {
        if (bt.compareTo(et) > 0) {
            throw new IllegalArgumentException("Begin time larger"
                    + " than end time");
        }
        beginTime = bt;
        endTime = et;
        length = beginTime.difference(et);
    }

    @Override
    public ITime getBeginTime() {
        return beginTime;
    }

    @Override
    public ITime getEndTime() {
        return endTime;
    }          

    @Override
    public int length() {
        return length;
    }

    @Override
    public void setBeginTime(ITime beginTime) {
                if (beginTime.compareTo(this.getEndTime()) > 0)
            throw new IllegalArgumentException("Begin time larger"
                    + " than end time");
        this.beginTime = beginTime;
    }

    @Override
    public void setEndTime(ITime endTime) {
                if (this.beginTime.compareTo(endTime) > 0)
            throw new IllegalArgumentException("Begin time larger"
                    + " than end time");
        this.endTime = endTime;
    }

    @Override
    public void move(int minutes) {
                this.beginTime.plus(minutes);
        this.length += minutes;
    }

    @Override
    public void changeLengthWith(int minutes) {
                int m = this.length + minutes;
        if (m<0)
           throw new IllegalArgumentException("Begin time larger"
                    + " than end time");
        this.length = m;
    }

    @Override
    public boolean isPartOf(IPeriod period) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public IPeriod unionWith(IPeriod period) {
        if (this.intersectionWith(period) == null) {
            return null;
        }
        ITime beginA = this.getBeginTime();
        ITime endA = this.getEndTime();
        ITime beginB = period.getBeginTime();
        ITime endB = period.getEndTime();
        if (beginA.compareTo(beginB) >= 0) {
            if (endA.compareTo(endB) >= 0) {
                return new Period2(beginB, endA);
            }
            return new Period2(beginB, endB);
        } else {
            if (endA.compareTo(endB) >= 0) {
                return new Period2(beginA, endA);
            }
            return new Period2(beginA, endB);
        }
    }

    @Override
    public IPeriod intersectionWith(IPeriod period) {
        ITime beginA = this.getBeginTime();
        ITime endA = this.getEndTime();
        ITime beginB = period.getBeginTime();
        ITime endB = period.getEndTime();
        //a >= b
        if (beginA.compareTo(beginB) >= 0) {
            if (endA.compareTo(endB) >= 0 && beginA.compareTo(endB) <= 0) {
                return new Period(beginA, endB);
            } else if (endA.compareTo(endB) <= 0) {
                return new Period(beginA, endA);
            }
        } else //b<a
        {
            if (endB.compareTo(endA) >= 0 && beginB.compareTo(endA) <= 0) {
                return new Period(beginB, endA);
            } else if (endB.compareTo(endA) <= 0) {
                return new Period(beginB, endB);
            }
        }
        return null;
    }
    
}
